﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Timeline.Beans;
using Timeline.Inis;
using Timeline.Utils;
using Windows.ApplicationModel.Resources;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// https://go.microsoft.com/fwlink/?LinkId=234238 上介绍了“内容对话框”项模板

namespace Timeline.Pages {
    public sealed partial class AccountDlg : ContentDialog {
        private readonly Ini ini = new Ini();

        public AccountDlg(Ini ini, string msg, bool success) {
            this.InitializeComponent();
            this.ini = ini;
            BoxAccountUser.Text = ini?.User ?? "";
            BoxAccountPwd.Password = ini?.Pwd ?? "";
            if (!string.IsNullOrEmpty(msg)) {
                if (success) {
                    TextAccountMsg1.Text = msg;
                    TextAccountMsg1.Visibility = Visibility.Visible;
                } else {
                    BoxAccountPwd.Visibility = Visibility.Visible;
                    TextAccountMsg0.Text = msg;
                    TextAccountMsg0.Visibility = Visibility.Visible;
                }
            }
        }

        private void BoxAccountUser_TextChanged(object sender, TextChangedEventArgs e) {
            BoxAccountPwd.Visibility = Visibility.Visible;
        }

        private void BoxAccountUser_GotFocus(object sender, RoutedEventArgs e) {
            BoxAccountPwd.Visibility = Visibility.Visible;
        }

        private async void DlgAccount_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args) {
            ini.User = BoxAccountUser.Text.Trim();
            ini.Pwd = !string.IsNullOrEmpty(ini.User) ? BoxAccountPwd.Password : "";
            await IniUtil.SaveUserPwdAsync(ini.User, ini.Pwd);
        }
    }
}
